import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/page/live/hot/Hot.dart';
import 'package:qgchat/page/live/newPeople/NewPeople.dart';
import 'package:qgchat/page/live/square/index.dart';
import 'package:qgchat/page/live/useractivity/index.dart';
import 'package:qgchat/states/states_model/user.dart';
import 'package:qgchat/widgets/tipsDialog.dart';

/**
 * 直播推荐页
 * page推荐
 */

class LiveTabBar extends StatefulWidget {
  LiveTabBar({Key key}) : super(key: key);

  @override
  _LiveTabBarState createState() => _LiveTabBarState();
}

class _LiveTabBarState extends State<LiveTabBar> with TickerProviderStateMixin {
  TabController mController;

  List<String> userOnline = [];
  List<Widget> userContent = [];

  List<String> tabTitles = [
    "新人",
    "热门",
    // "同城",
    "广场",
    // "试看",
    // "在线",
  ];
  List<String> tabTitlesAnthor = [
    "动态",
    "新人",
    "热门",
    // "同城",
    "广场",
    // "在线",
  ];

  List<Widget> viewList = [
    NewPeople(),
    Hot(),
    // SameCity(),
    Square(),
    // TryToSee(),
    // OnLine(),
  ];
  List<Widget> viewListAnthor = [
    Useractivity(),
    NewPeople(),
    Hot(),
    // SameCity(),
    Square(),
    // OnLine(),
  ];

  @override
  void initState() {
    super.initState();

    Timer(Duration(milliseconds: 500), () {
      setState(() {});
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 监听用户信息改变
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }

  // 生成body
  Widget bodyHandle() {
    return Container(
      width: flex1,
      alignment: Alignment.topCenter,
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      child: tabBarViewHandle(userContent),
    );
  }

  // tabBar
  tabBarHandle() {
    return TabBar(
      isScrollable: true,
      controller: mController,
      labelColor: col("303133"),
      labelPadding: EdgeInsets.symmetric(horizontal: px(34)),
      labelStyle: TextStyle(
        fontWeight: FontWeight.bold,
      ),
      unselectedLabelStyle: TextStyle(fontSize: px(34)),
      unselectedLabelColor: col("303133"),
      indicatorColor: col("d35edd"),
      indicatorSize: TabBarIndicatorSize.label,
      indicatorWeight: px(6),
      tabs: userOnline.map<Tab>((item) {
        return Tab(text: item);
      }).toList(),
    );
  }

  // 生成搜索
  Widget searchHandle() {
    return IconButton(
      icon: ImageIcon(
        AssetImage("assets/images/icon/search.png"),
        color: Colors.black,
        size: px(34),
      ),
      onPressed: () {
        final user = Provide.value<UserState>(context);
        final info = user.info;
        final isLogin = info != null;
        if (isLogin) {
          goPage(context, "/search");
        } else {
          TipsDialog.login(
            context,
            () {
              goPage(context, '/phoneLogin');
            },
          );
        }
      },
    );
  }

  // 生成tabBarView
  TabBarView tabBarViewHandle(tabcontent) {
    return TabBarView(
      controller: mController,
      children: tabcontent,
    );
  }

  @override
  Widget build(BuildContext context) {
    final type = Provide.value<UserState>(context)?.info?.memberType;
    if (type == 1) {
      userOnline = tabTitlesAnthor;
      userContent = viewListAnthor;
    } else {
      userOnline = tabTitles;
      userContent = viewList;
    }
    mController = TabController(length: userOnline.length, vsync: this);

    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(
          px(116 + MediaQueryData.fromWindow(window).padding.top),
        ),
        child: Container(
          height: px(116 + MediaQueryData.fromWindow(window).padding.top),
          padding: EdgeInsets.only(
            top: MediaQueryData.fromWindow(window).padding.top,
          ),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: col("fafafa"),
            border: Border(
              bottom: BorderSide(
                color: col("edeff2"),
                width: 1,
              ),
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Expanded(
                child: Container(
                  height: px(66),
                  child: tabBarHandle(),
                ),
              ),
              searchHandle(),
            ],
          ),
        ),
      ),
      body: bodyHandle(),
    );
  }
}
